<template>
    <div class="review-container">
      <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch">
        <el-form-item label="用户ID" prop="userId">
          <el-input v-model="queryParams.userId" placeholder="请输入用户ID" clearable style="width: 200px" @keyup.enter="handleQuery" />
        </el-form-item>
        <el-form-item label="商品/服务ID" prop="itemId">
          <el-input v-model="queryParams.itemId" placeholder="请输入商品/服务ID" clearable style="width: 200px" @keyup.enter="handleQuery" />
        </el-form-item>
        <el-form-item label="评分" prop="rating">
          <el-select v-model="queryParams.rating" placeholder="选择评分" clearable style="width: 200px">
            <el-option v-for="num in [1, 2, 3, 4, 5]" :key="num" :label="num" :value="num" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
          <el-button icon="Refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
  
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button type="primary" plain icon="Plus" @click="handleAdd">新增评价</el-button>
        </el-col>
      </el-row>
  
      <el-table v-if="refreshTable" v-loading="loading" :data="reviewList" row-key="id">
        <el-table-column prop="userId" label="用户ID" width="120"></el-table-column>
        <el-table-column prop="itemId" label="商品/服务ID" width="120"></el-table-column>
        <el-table-column prop="entityType" label="实体类型" width="120"></el-table-column>
        <el-table-column prop="rating" label="评分" width="100"></el-table-column>
        <el-table-column prop="comment" label="评价内容" width="300"></el-table-column>
        <el-table-column prop="date" label="评价日期" width="180">
          <template #default="scope">
            <span>{{ formatDate(scope.row.date) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)">修改</el-button>
            <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
  
      <el-dialog :title="title" v-model="open" width="600px" append-to-body>
        <el-form ref="reviewRef" :model="form" :rules="rules" label-width="80px">
          <el-row>
            <el-col :span="12">
              <el-form-item label="用户ID" prop="userId">
                <el-input v-model="form.userId" placeholder="请输入用户ID" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="商品/服务ID" prop="itemId">
                <el-input v-model="form.itemId" placeholder="请输入商品/服务ID" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="实体类型" prop="entityType">
                <el-select v-model="form.entityType" placeholder="选择实体类型">
                  <el-option label="商品" value="product" />
                  <el-option label="商店" value="store" />
                  <el-option label="服务" value="service" />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="评分" prop="rating">
                <el-select v-model="form.rating" placeholder="选择评分">
                  <el-option v-for="num in [1, 2, 3, 4, 5]" :key="num" :label="num" :value="num" />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="评价内容" prop="comment">
                <el-input type="textarea" v-model="form.comment" placeholder="请输入评价内容" />
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <template #footer>
          <div class="dialog-footer">
            <el-button type="primary" @click="submitForm">确 定</el-button>
            <el-button @click="cancel">取 消</el-button>
          </div>
        </template>
      </el-dialog>
    </div>
  </template>
  
  <script setup name="ReviewList">
  import { ref, reactive } from 'vue';
  import { listReviews, addReview, updateReview, deleteReview } from "@/api/pages/review";
  
  const loading = ref(true);
  const open = ref(false);
  const showSearch = ref(true);
  const title = ref("");
  const refreshTable = ref(true);
  const reviewList = ref([]);
  const form = reactive({
    id: undefined,
    userId: "",
    itemId: "",
    entityType: "product",
    rating: 1,
    comment: "",
  });
  const queryParams = reactive({
    userId: "",
    itemId: "",
    rating: undefined,
  });
  const rules = {
    userId: [{ required: true, message: "用户ID不能为空", trigger: "blur" }],
    itemId: [{ required: true, message: "商品/服务ID不能为空", trigger: "blur" }],
    rating: [{ required: true, message: "评分不能为空", trigger: "change" }],
    comment: [{ required: true, message: "评价内容不能为空", trigger: "blur" }],
  };
  
  // 查询评价列表
  const getList = () => {
    loading.value = true;
    listReviews(queryParams).then(response => {
      reviewList.value = response.data;
      loading.value = false;
    });
  };
  
  // 取消按钮
  const cancel = () => {
    open.value = false;
    reset();
  };
  
  // 重置表单
  const reset = () => {
    Object.assign(form, {
      id: undefined,
      userId: "",
      itemId: "",
      entityType: "product",
      rating: 1,
      comment: "",
    });
  };
  
  // 搜索按钮操作
  const handleQuery = () => {
    getList();
  };
  
  // 重置按钮操作
  const resetQuery = () => {
    Object.assign(queryParams, {
      userId: "",
      itemId: "",
      rating: undefined,
    });
    getList();
  };
  
  // 新增按钮操作
  const handleAdd = () => {
    reset();
    open.value = true;
    title.value = "新增评价";
  };
  
  // 修改按钮操作
  const handleUpdate = (row) => {
    Object.assign(form, row);
    open.value = true;
    title.value = "修改评价";
  };
  
  // 提交按钮
  const submitForm = () => {
    const apiCall = form.id ? updateReview : addReview;
    apiCall(form).then(() => {
      getList();
      open.value = false;
    });
  };
  
  // 删除按钮操作
  const handleDelete = (row) => {
    deleteReview(row.id).then(() => {
      getList();
    });
  };
  
  // 格式化日期
  const formatDate = (date) => {
    return new Date(date).toLocaleString();
  };
  
  // 初始化评价列表
  getList();
  </script>
  
  <style scoped>
  .review-container {
    padding: 20px;
  }
  .mb8 {
    margin-bottom: 8px;
  }
  .dialog-footer {
    text-align: right;
  }
  </style>